<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\common\model;
use think\Model;
use think\facade\Db;
use think\facade\Request;
class MemberLoginModel extends Model{
    protected $name = 'member_login';
    protected $pk = 'login_id';
    //修改用户最后一次的登录信息
    public function SetLastLoginInfo(int $member_id){
        $member_r=Db::name("member")->find($member_id);
        //更新用户最后登录信息
        Db::name("member")->update(array(
            "last_login_ip"=>get_client_ip(),
            "last_login_time"=>time(),
            "login_num"=>$member_r['login_num']+1,
            "member_id"=>$member_id,
        ));
    }
    //设置登录信息
    public function SetLoginToken($member_id,$device="pc",$openid=""){
        $where=[];
        $where["member_id"]=$member_id;
        $where["device"]=$device;
        //查找对应的设备登录信息是否存在
        $info=$this->where($where)->find();
        $token=make_random(32);
        //
        $data=[
            "member_id"=>$member_id,
            "device"=>$device,
            "token"=>$token,
            "token_time"=>time(),
            "last_login_ip"=>get_client_ip(),
            "last_login_time"=>time(),
            "open_id"=>$openid,
        ];
        if($info){
            $data["login_id"]=$info["login_id"];
            $this->update($data);
        }else{
            $this->save($data);
        }
        return $token;
    }
}